const UserService = require('../../services/admin/UserSevervice');
const JWT = require('../../util/JWT');

const UserController = {
    //登录
    login: async (req, res) => {
        let result = await UserService.login(req.body);
        // 登录校验
        if (result.length === 0) {
            res.send({
                code: '-1', //返回信息
                error: '用户名密码不匹配',
            });
        } else {
            // 生成token(用户信息,有效时间1天)
            const token = JWT.generate(
                {
                    _id: result[0]._id,
                    username: result[0].username,
                },
                '1d'
            );
            //传给前端
            res.header('Authorization', token); //同时将token在响应头中返回给前端
            res.send({
                ActionType: 'OK',
                data: {
                    _id: result[0]._id,
                    //给前端 可以提前解构出来
                    username: result[0].username,
                    gender: result[0].gender ? result[0].gender : 0, //性别 ,0[保密],1[男],2[女]
                    introduction: result[0].introduction, //简介
                    avatar: result[0].avatar, // 头像
                    role: result[0].role, //管理员1 ,编辑2
                },
            });
        }
    },
    //修改用户信息
    upload: async (req, res) => {
        const { username, introduction, gender } = req.body;
        const token = req.headers['authorization'].split(' ')[1];
        //判断用户是否上传头像 上传了读取filename 没上传直接为空
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : '';
        var payload = JWT.verify(token);
        //调用service 模块更新 数据

        await UserService.upload({ _id: payload._id, username, introduction, gender: Number(gender), avatar });
        if (avatar) {
            res.send({
                ActionType: 'OK',
                data: {
                    username,
                    introduction,
                    gender: Number(gender),
                    avatar,
                },
            });
        } else {
            res.send({
                ActionType: 'OK',
                data: {
                    username,
                    introduction,
                    gender: Number(gender),
                },
            });
        }
    },
    //创建新的用户
    add: async (req, res) => {
        const { username, introduction, gender, role, password } = req.body;
        //判断用户是否上传头像 上传了读取filename 没上传直接为空
        const avatar = req.file ? `/avataruploads/${req.file.filename}` : '';
        //调用service 模块更新 数据
        await UserService.add({ username, introduction, gender: Number(gender), role: Number(role), password, avatar });
        res.send({
            ActionType: 'OK',
        });
    },
    //获取用户列表
    getList: async (req, res) => {
        const result = await UserService.getList(req.params);
        res.send({
            ActionType: 'OK',
            data: result,
        });
    },
    //删除用户
    delList: async (req, res) => {
        const _id = req.params.id;
        const result = await UserService.delList({ _id });
        res.send({
            ActionType: 'OK',
        });
    },
    //更新用户信息 单条
    putList: async (req, res) => {
        const id = req.params.id;
        // console.log(id);
        //这里的req.body里有id属性 不再重复定义传参
        const result = await UserService.putList(id, req.body);
        res.send({
            ActionType: 'OK',
        });
    },
};

module.exports = UserController;
